module;

#include <Windows.h>

export module winapi:windows;

import std.core;

struct EnumWndProcParam {
	DWORD cur_pid;
	HWND hwnd;
	std::string clz_name;
};

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam) {
	EnumWndProcParam param = *((EnumWndProcParam*)lparam);
	DWORD cur_pid = param.cur_pid;
	DWORD pid = 0;

	GetWindowThreadProcessId(hwnd, &pid);
	char clz_name[50]{ 0 };
	GetClassNameA(hwnd, clz_name, 50);
	if(strcmp(clz_name, param.clz_name.c_str()) == 0 && pid == cur_pid && GetParent(hwnd) == NULL)
    {
		param.hwnd = hwnd;
        *((EnumWndProcParam*)lparam) = param;
        return FALSE;
    }

    return TRUE;
}
 
 
export HWND get_main_wnd(std::string clz_name) {
	EnumWndProcParam param;
	param.cur_pid = GetCurrentProcessId();
	param.hwnd = NULL;
	param.clz_name = clz_name;
	if (!EnumWindows(EnumWindowsProc, (LPARAM)&param)) {
		return param.hwnd;
	}
	return NULL;
}
